﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Fish_Atlantis
{
    public partial class Chart : System.Web.UI.Page
    {
        DataSet ds, ds2;
        string usetype;
        public string datastr, datastr2, sentence1, sentence2, tmpp;
        public string[] tdt1 = new string[30];
        public string[] tdt2 = new string[4];

        public int count1 = 0, count2 = 4, cx;
        protected void Page_Load(object sender, EventArgs e)
        {
            string str, tmp, str2, str3;
            double r1 = 0, r2 = 0, r3 = 0, r4 = 0, s = 0;
            datastr = "[";
            if (Session["UserID"] != null)
            {
                usetype = Session["User_Type"].ToString();
                str2 = "select  Fishery.FisheryName,Risk.Comment from Fishery,Risk where  Fishery.FisheryID=Risk.FisheryID and  Risk.Month=(MONTH(GETDATE())) and Fishery.FisheryID=";
                if (usetype == "1")
                {
                    sentence1 = "您当月消费的各种鱼总量统计";
                    sentence2 = "您当月消费的各种鱼占各风险等级的比例";
                    str = "select Fishery.FisheryID as fisheryid,Fishery.FisheryName as fisheryname,Risk.Risk_Rank as frank,p.amount as amount from (select Transaction_Record.FisheryID as FisheryID,SUM(Transaction_Record.Amount) as amount from Users,Transaction_Record where Users.UserID=Transaction_Record.CustomerID and Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Users.UserID= " + Session["UserID"].ToString() + " group by (Transaction_Record.FisheryID)) as p,Fishery,Risk where p.FisheryID=Fishery.FisheryID and p.FisheryID=Risk.FisheryID and Risk.Month=(MONTH(GETDATE()))";

                    str3 = "select customer.UserName as customer,supplier.UserName as supplier,Fishery.FisheryName as fisheryname,MONTH(GETDATE()) as period,Transaction_Record.Amount as amount,Risk.Risk_Rank as frank from Transaction_Record,Fishery,Risk,Users as customer,Users as supplier where  Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Risk.Month=(MONTH(GETDATE())) and Transaction_Record.CustomerID=customer.UserID and Transaction_Record.SupplierID=supplier.UserID and Transaction_Record.FisheryID=Fishery.FisheryID and Transaction_Record.FisheryID=risk.FisheryID and Transaction_Record.CustomerID= " + Session["UserID"].ToString();
                }
                else if (usetype == "2")
                {
                    sentence1 = "您当月捕捞的各种鱼总量统计";
                    sentence2 = "您当月捕捞的各种鱼占各风险等级的比例";
                    str = "select  Fishery.FisheryID as fisheryid,Fishery.FisheryName as fisheryname,Risk.Risk_Rank as frank,p.amount as amount from (select Transaction_Record.FisheryID as FisheryID,SUM(Transaction_Record.Amount) as amount from Users,Transaction_Record where Users.UserID=Transaction_Record.SupplierID and Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Users.UserID= " + Session["UserID"].ToString() + " group by (Transaction_Record.FisheryID)) as p,Fishery,Risk where p.FisheryID=Fishery.FisheryID and p.FisheryID=Risk.FisheryID and Risk.Month=(MONTH(GETDATE()))";
                    str3 = "select customer.UserName as customer,supplier.UserName as supplier,Fishery.FisheryName as fisheryname,MONTH(GETDATE()) as period,Transaction_Record.Amount as amount,Risk.Risk_Rank as frank from Transaction_Record,Fishery,Risk,Users as customer,Users as supplier where  Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Risk.Month=(MONTH(GETDATE())) and Transaction_Record.CustomerID=customer.UserID and Transaction_Record.SupplierID=supplier.UserID and Transaction_Record.FisheryID=Fishery.FisheryID and Transaction_Record.FisheryID=risk.FisheryID and Transaction_Record.SupplierID= " + Session["UserID"].ToString();
                }
                else
                {
                    sentence1 = "当月各种鱼总量统计";
                    sentence2 = "当月各种鱼占各风险等级的比例";
                    str = "select  Fishery.FisheryID as fisheryid,Fishery.FisheryName as fisheryname,Risk.Risk_Rank as frank,p.amount as amount from (select Transaction_Record.FisheryID as FisheryID,SUM(Transaction_Record.Amount) as amount from Users,Transaction_Record where Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Users.UserID= " + Session["UserID"].ToString() + " group by (Transaction_Record.FisheryID)) as p,Fishery,Risk where p.FisheryID=Fishery.FisheryID and p.FisheryID=Risk.FisheryID and Risk.Month=(MONTH(GETDATE()))";
                    str3 = "select customer.UserName as customer,supplier.UserName as supplier,Fishery.FisheryName as fisheryname,MONTH(GETDATE()) as period,Transaction_Record.Amount as amount,Risk.Risk_Rank as frank from Transaction_Record,Fishery,Risk,Users as customer,Users as supplier where  Month(Transaction_Record.Period)=(MONTH(GETDATE())) and Risk.Month=(MONTH(GETDATE())) and Transaction_Record.CustomerID=customer.UserID and Transaction_Record.SupplierID=supplier.UserID and Transaction_Record.FisheryID=Fishery.FisheryID and Transaction_Record.FisheryID=risk.FisheryID ";

                }
                ds = Helper.GetDataSet(str, "Users");
                count1 = ds.Tables[0].Rows.Count;
                if (count1 <= 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('无交易记录！');</script>");
                    return;
                }
                else
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        tmp = "";
                        if (i != 0)
                            tmp += ",";
                        s += double.Parse(ds.Tables[0].Rows[i]["amount"].ToString());
                        tmp += "{ country: \"" + ds.Tables[0].Rows[i]["fisheryname"].ToString() + "\",visits: " + ds.Tables[0].Rows[i]["amount"].ToString() + ",color:";
                        if (ds.Tables[0].Rows[i]["frank"] != null)
                        {
                            if (ds.Tables[0].Rows[i]["frank"].ToString() == "3")
                            {
                                tmp += "\"#FF0F00\"}";
                                r1 += double.Parse(ds.Tables[0].Rows[i]["amount"].ToString());
                            }
                            else if (ds.Tables[0].Rows[i]["frank"].ToString() == "2")
                            { tmp += "\"#F8FF01\"}"; r2 += double.Parse(ds.Tables[0].Rows[i]["amount"].ToString()); }
                            else if (ds.Tables[0].Rows[i]["frank"].ToString() == "1")
                            { tmp += "\"#04D215\"}"; r3 += double.Parse(ds.Tables[0].Rows[i]["amount"].ToString()); }
                            else
                            { tmp += "\"#f0f0f0\"}"; r4 += double.Parse(ds.Tables[0].Rows[i]["amount"].ToString()); }
                        }
                        datastr += tmp;


                        tmpp = str2;

                        tmpp += ds.Tables[0].Rows[i]["fisheryid"].ToString();
                        ds2 = Helper.GetDataSet(tmpp, "user");
                        cx = ds2.Tables[0].Rows.Count;
                        tdt1[i] = "";
                        if (cx > 0)
                        {
                            tdt1[i] += ds2.Tables[0].Rows[0]["FisheryName"].ToString();
                            if (ds.Tables[0].Rows[i]["frank"].ToString() == "3")
                            { tdt1[i] += "现在是高风险，原因："; tdt1[i] += ds2.Tables[0].Rows[0]["Comment"]; }
                            else if (ds.Tables[0].Rows[i]["frank"].ToString() == "2")
                            { tdt1[i] += "现在是中风险，原因："; tdt1[i] += ds2.Tables[0].Rows[0]["Comment"]; }
                            else
                               tdt1[i] += "现在是低风险";
                            
                        }
                    }
                    string[] k = { "3", "2", "1", "4" };
                    for (int i = 0; i < 4; i++)
                    {
                        tmpp = str3;
                        if (k[i]!= "4")
                        { tmpp += " and Risk.Risk_Rank="; tmpp += k[i]; }
                        ds2 = Helper.GetDataSet(tmpp, "user");
                        cx = ds2.Tables[0].Rows.Count;
                        tdt2[i] = "<table><tr><td>买家     </td><td>卖家     </td><td>鱼名       </td><td>交易季度</td><td>总量</td><td>风险等级</td></tr>";
                        if (cx > 0)
                        {
                            for (int j = 0; j < cx; j++)
                            {
                                tdt2[i] += "<tr><td>";
                                tdt2[i] += ds2.Tables[0].Rows[j]["customer"].ToString() + "</td><td>" + ds2.Tables[0].Rows[j]["supplier"].ToString() + "</td><td>" + ds2.Tables[0].Rows[j]["fisheryname"].ToString() + "</td><td>" + ds2.Tables[0].Rows[j]["period"].ToString() + "</td><td>" + ds2.Tables[0].Rows[j]["amount"].ToString() + "</td><td>";
                                if (k[i].ToString() == "3")
                                {
                                    tdt2[i] += "高风险</td></tr>";
                                }
                                else if (k[i].ToString() == "2")
                                {
                                    tdt2[i] += "中风险</td></tr>";
                                }
                                else if (k[i].ToString() == "1")
                                {
                                    tdt2[i] += "低风险</td></tr>";
                                }
                                else
                                {
                                    tdt2[i] += "未评价</td></tr>";
                                }
                            }
                            tdt2[i] += "</table>";
                        }
                    }

                    datastr += "]";
                    if (s != 0)
                    {
                        r1 /= s;
                        r2 /= s;
                        r3 /= s;
                        r4 /= s;
                    }
                    datastr2 = "[{ country: \"高风险\",value:" + r1.ToString() + ",color: \"#FF0F00\"},{ country: \"中风险\",value:" + r2.ToString() + ",color: \"#F8FF01\"},{ country: \"低风险\",value:" + r3.ToString() + ",color: \"#04D215\"},{ country: \"未评级\",value:" + r4.ToString() + ",color: \"#f0f0f0\"}]";
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('您还未登录，请登录后再操作！！');location.href='/Account/Login.aspx';</script>");
            }
        }
    }
}